﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.Helpers;
using System.Data;
using SYcwpb.ViewModels;
using SYcwpb.ViewModels.DataProvider;
using System.Collections.ObjectModel;
using System.Collections;


namespace SYcwpb.Views
{
    /// <summary>
    /// FilterDrivers.xaml 的交互逻辑
    /// </summary>
    public partial class FilterDrivers : Window
    {
        BedRoomsViewModel viewModel;
        ObservableCollection<YBDriver> oldDrivers=new ObservableCollection<YBDriver>();
        public FilterDrivers(BedRoomsViewModel view)
        {
            this.DataContext = viewModel = view;
            for (int i = 0; i < view.DriverList.Count; i++)
            {
                oldDrivers.Add(view.DriverList[i]);
            }
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

            }
            catch (System.Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IList list = listBox1.SelectedItems;
                if (list == null)
                    return;
                //foreach (YBDriver item in list)
                for (int i = 0; i < list.Count;i++ )
                {
                    YBDriver item = (YBDriver)list[i];
                    viewModel.DriverList.Remove(item);
                    viewModel.UnarrangeDriverList.Add(item);
                }
                listBox1.SelectedItems.Clear();
                
            }
            catch (System.Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IList list = listBox2.SelectedItems;
                if (list == null)
                    return;
                for (int i = 0; i < list.Count; )
                {
                    YBDriver item = (YBDriver)list[i];
                    //viewModel.DriverList.Add(item);
                    viewModel.UnarrangeDriverList.Remove(item);
                    
                }
                viewModel.DriverList.Clear();
                for (int i = 0; i < oldDrivers.Count; i++)
                {
                    viewModel.DriverList.Add(oldDrivers[i]);
                }
                foreach (YBDriver item in viewModel.UnarrangeDriverList)
                {
                    viewModel.DriverList.Remove(item);
                }

                listBox2.SelectedItems.Clear();
            }
            catch (System.Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                viewModel.FilterDriversResult = true;
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }
    }
}
